﻿/****************************************************
    文件：#SCRIPTNAME#.cs
	作者：GHC
    邮箱: 1191775201@qq.com
    日期：#CreateTime#
	功能：角色脚本
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Player : MonoBehaviour
{
    public ChessType chessColor = ChessType.Black;
    private Button ReturnBtn;
    bool isDoubleMode = false;

    protected virtual void Start()
    {
        ReturnBtn = GameObject.Find("RetractChess").GetComponent<Button>();
        if (PlayerPrefs.GetInt("Double") == 10)
            isDoubleMode = true;
    }

    protected virtual void FixedUpdate()
    {
        if(chessColor==ChessBoard.Instance.turn&& ChessBoard.Instance.timer > 0.5f)
        {
            PlayerChess();
        }

        if(!isDoubleMode)
        ChangeBtnColor();
    }

    public virtual void PlayerChess()
    {
        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
        {
            Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            //print((int)(pos.x + 7.5f)+ " " + (int)(pos.y + 7.5f));
            if (ChessBoard.Instance.PlayChess(new int[2] { (int)(pos.x + 7.5f), (int)(pos.y + 7.5f) }))
                ChessBoard.Instance.timer = 0;
        }
    }

    protected virtual void ChangeBtnColor()
    {
        if (chessColor == ChessType.Watch)
            return;
        if (ChessBoard.Instance.turn == chessColor)
            ReturnBtn.interactable = true;
        else
            ReturnBtn.interactable = false;
    }
}
